package common;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SynUtils {


    public final static String fileMapStorage ="fileMap";
    public final static String blockMapStorage="blockMap";
    public final static String dNListStorage="dNList";

    public static void writeBlockMap(BlockInfo blockInfo) throws IOException {
        List<DNInfo> dnInfos = blockInfo.getLocations();
        StringBuilder sb = new StringBuilder();
        sb.append(blockInfo).append(",");
        for (DNInfo dnInfo : dnInfos) {
            sb.append(dnInfo.getIp()).append("#").
                    append(dnInfo.getPort()).append("#")
                    .append(dnInfo.getAlive());
            if(dnInfos.indexOf(dnInfo)!=dnInfos.size()-1){
                sb.append("@");
            }
        }
        FileWriter fw = new FileWriter(blockMapStorage,true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(sb.toString()+"\n");
        bw.close();
        fw.close();
    }

    public static List<BlockInfo> readBlockMap() throws IOException {
        List<BlockInfo> list = new ArrayList<>();
        File file = new File(blockMapStorage);
        if(!file.exists()){
            return list;
        }
        FileReader fr = new FileReader(blockMapStorage);
        BufferedReader br = new BufferedReader(fr);
        String line=null;
        while((line=br.readLine())!=null){
            String[] split = line.split(",");
            int blockNum = Integer.parseInt(split[0]);
            String filename = split[1];
            int status = Integer.parseInt(split[2]);
            int validLen = Integer.parseInt(split[3]);
            long CRC = Long.parseLong(split[4]);
            List<DNInfo> dnInfos = new ArrayList<>();
            String[] split1 = split[5].split("@");
            for (String s : split1) {
                String[] split2 = s.split("#");
                String IP = split2[0];
                int PORT = Integer.parseInt(split2[1]);
                boolean isAlive = "true".equals(split2[2]);
                DNInfo dnInfo = new DNInfo(IP,PORT,isAlive);
                dnInfos.add(dnInfo);
            }
            BlockInfo blockInfo = new BlockInfo(blockNum,filename,status,dnInfos,validLen,CRC);
            list.add(blockInfo);
        }
        return list;
    }

    public static void writeFileInfo(FileInfo fileInfo) throws IOException {
        List<Integer> blockList = fileInfo.getBlockList();
        StringBuilder sb = new StringBuilder();
        sb.append(fileInfo.getFileName()).append(",")
                .append(fileInfo.getStatus()).append(",");
        for (int i=0;i<blockList.size();i++) {
            if(i!=blockList.size()-1){
                sb.append(blockList.get(i)).append("#");
            }
            else sb.append(blockList.get(i));
        }
        FileWriter fw = new FileWriter(fileMapStorage,true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(sb.toString()+"\n");
        bw.close();
        fw.close();
    }

    public static List<FileInfo> readFileInfo() throws IOException {
        List<FileInfo> list = new ArrayList<>();
        File file = new File(fileMapStorage);
        if(!file.exists()){
            return list;
        }
        FileReader fr = new FileReader(fileMapStorage);
        BufferedReader br = new BufferedReader(fr);

        String line=null;
        while((line=br.readLine())!=null){
            String[] split = line.split(",");
            String filename = split[0];
            int status = Integer.parseInt(split[1]);
            String[] split1 = split[2].split("#");
            List<Integer> blocks = new ArrayList<>();
            for (String s : split1) {
                blocks.add(Integer.parseInt(s));
            }
            FileInfo fileInfo = new FileInfo(filename,blocks,status);
            list.add(fileInfo);
        }
        return list;
    }

    public static void writedNList(DNInfo dnInfo) throws IOException {
        String s = dnInfo.getIp()+","+dnInfo.getPort()+","+dnInfo.gettPort()+","+dnInfo.getAlive();
        FileWriter fw = new FileWriter(dNListStorage,true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(s+"\n");
        bw.close();
        fw.close();
    }

    public static List<DNInfo> readdNList() throws IOException {
        List<DNInfo> list = new ArrayList<>();
        File file = new File(dNListStorage);
        if(!file.exists()){
            return list;
        }
        FileReader fr = new FileReader(dNListStorage);
        BufferedReader br = new BufferedReader(fr);

        String line=null;
        while((line=br.readLine())!=null){
            String[] split = line.split(",");
            String IP = split[0];
            int PORT = Integer.parseInt(split[1]);
            int TPORT = Integer.parseInt(split[2]);
            DNInfo dnInfo = new DNInfo(IP, PORT,TPORT);
            list.add(dnInfo);
        }
        return list;
    }
}
